<template>
  <ele-page>
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        border
        stripe
      >
        <template #toolbar>
          <el-button
            type="success"
            class="ele-btn-icon"
            :icon="Plus"
            @click="openEdit()"
          >
            新建
          </el-button>
        </template>
        <template #action="{ row }">
          <span v-if="row.status === 9999">/</span>
          <el-button-group v-else>
            <el-button type="warning" size="small" @click="openEdit(row)"
              >编辑</el-button
            >

            <el-button type="danger" size="small" @click="del(row)"
              >删除</el-button
            >
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <edit v-model="showEdit" :data="current" @done="reload" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import { Plus } from '@element-plus/icons-vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessageBox } from 'element-plus/es';
  import { pageVehicleIncomeExpenseCate, delVehicleIncomeExpenseCate } from '@/api/company';
  import Edit from './components/edit.vue';
  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'id',
      label: 'ID',
      width: 85,
      align: 'center'
    },
    {
      prop: 'title',
      label: '分类名称'
    },

    {
      columnKey: 'action',
      label: '操作',
      slot: 'action',
      width: 150,
      fixed: 'right'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageVehicleIncomeExpenseCate({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  // 审核
  const del = (row) => {
    ElMessageBox.confirm('确定要删除' + row.title + '吗？', '系统提示', {
      type: 'warning',
      draggable: true
    })
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        delVehicleIncomeExpenseCate({ id: row.id })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
            reload();
          })
          .catch((e) => {
            loading.close();
            EleMessage.error(e.message);
          });
      })
      .catch(() => {});
  };
</script>
